package main

import (
	"fmt"
	"math/rand"
	"strconv"
	"time"
)

// 1. 简单工厂
/*
	定义 接口 Fruit 和 三个实现类 Orange， Strawberry， Cherry
*/
type Fruit interface {
	Eat()
}

type Orange struct {
	name string
}

func newOrange(name string) Fruit {
	return &Orange{
		name: name,
	}
}

func (o *Orange) Eat() {
	fmt.Printf("I am orange: %s, I am about to be eaten...", o.name)
}

type Strawberry struct {
	name string
}

func NewStrawberry(name string) Fruit {
	return &Strawberry{
		name: name,
	}
}

func (s *Strawberry) Eat() {
	fmt.Printf("i am strawberry: %s, i am about to be eaten...", s.name)
}

type Cherry struct {
	name string
}

func NewCherry(name string) Fruit {
	return &Cherry{
		name: name,
	}
}

func (c *Cherry) Eat() {
	fmt.Printf("i am cherry: %s, i am about to be eaten...", c.name)
}

/*
生产水果的工厂类 FruitFactory
*/
type FruitFactory struct {
}

func NewFruitFactory() *FruitFactory {
	return &FruitFactory{}
}

func (f *FruitFactory) CreateFruit(typ string) (Fruit, error) {
	src := rand.NewSource(time.Now().UnixNano())
	rander := rand.New(src)
	name := strconv.Itoa(rander.Int())

	switch typ {
	case "orange":
		return newOrange(name), nil
	case "strawberry":
		return NewStrawberry(name), nil
	case "cherry":
		return NewCherry(name), nil
	default:
		return nil, fmt.Errorf("fruit typ: %s is not supported yet", typ)
	}
}

func main() {

}
